Devastating 'rm' Command

Author: Piyi Zu
Release Time: 2024-03-31T17:56+0800
Tag(s): opinion
Category: Miscellaneous


'rm' --- A Very Dangerous Command

It looks cool that you operate your computer in command-line mode in front of folks who know little about computers. It does unless you type a wrong 'rm' command and press Enter key without even noticing your command would destroy the computer. The program runs so fast that when you realise your computer commits a suicide, it's all too late...

The command-line is a very old way to operate computers. Before graphical user interfaces(GUIs) were invented, it was the most convenient way of all. It has advantages: simple, clean and fast. It can be seen now on most servers because it comsumes few resources and seldom crashes the operating system. Some programmers like to do their work in command-line environments because it contains nothing that may distract or upset them, while in GUI environments they usually have annoyingly piled windows and a messy taskbar. However, one should be very careful when typing commands into their computer, as there exists a very dangerous but common command --- 'rm'.

You may have heard of a joke that if one wants their Linux computer to run even faster, they can simply execute 'sudo rm / -rf' in the terminal, then sit back and relax. This command is most harmful. It removes all the files in the computer without a pause. People who know even a little about computers wouldn't type commands like that into the computer, but sometimes due to carelessness, they do make mistakes.

Some Bad Experiences of My Improperly Using 'rm'

I've unfortunately made such mistakes twice in my life and I promised to myself they NEVER happen again.

The first time I screwed things up was a year ago when I was preparing to develop a new application on a small computer like a Raspberry Pi. The system on the computer was oversimplifed and I had to log in as a super user. The development needed a lot of files but the computer then had no Internet access, so I had got to copy files manually and switch among different directories. When copying files, it's common to be in a directory where you can get the source and the destination without accessing the parent directory. Thus I changed my working directory to be the root.

After a wrong copy, I decided to remove some files and repeat the copy with correct arguments. But I forgot I was in the root directory and I executed 'rm ./* -rf'. I didn't notice that I'd done something wrong until I found many files that couldn't be in the copied files were also deleted. I sent a quick Ctrl+C to terminate the execution, but the last printed message told me that almost all the files of the computer had been smashed into ash. I tried to run 'ls' but the computer didn't know what 'ls' was. I restarted the computer and it ran, as you may guess, into a black screen. I spent seveal weeks repairing it as there was hardly anything about the product online.

Another time when I was cleaning my project source directory, I executed 'rm ./* -r' in some folder. But for some unknown reason, the dot was missing. The command became 'rm /* -r'. I nearly jumped from my seat when I was asked if it should go into the write-protected '/boot' directory to delete more files. No man steps into the same river twice. However, I made things wrong again when executing 'rm' command. Nevertheless, I was lucky this time, for I executed the command as a common user and no file seemed to be removed.

Lessons Taught and Learnt

  1. Never log in as a super user
  2. Never execute 'rm' command in the root directory unless necessary
  3. Never use '-f' options when removing files with 'rm'
  4. Always check the command arguments and the current working directory before pressing Enter
  5. Recommended to add '-i' or '-I' option when using 'rm'

Also, when writing a program to delete a large number of files, be careful and don't forget to replace the removal commands with print()'s when testing and absolute pathnames are better and safer than relative ones.

It's always right to be careful and focused when working on the computers.